package com.d17_homework.homework1;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Demo1 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //创建泛型为Integer的集合对象
        ArrayList<Integer> list = new ArrayList<>();
        //获取集合的字节码对象
        Class<? extends ArrayList> clazz = list.getClass();
        //获取添加的方法，方法名，方法的参数时Object类型
        Method method = clazz.getDeclaredMethod("add", Object.class);
        //暴力反射
        method.setAccessible(true);
        //添加元素
        Object o1 = method.invoke(list, "aaa");
        Object o2 = method.invoke(list, "bbb");
        Object o3 = method.invoke(list, 100);
        //遍历集合打印
        for (Object obj : list) {
            System.out.println(obj);
        }

    }
}
